package com.enbatis.mybatisplugs.service.impl;

import com.enbatis.mybatisplugs.base.Account;
import com.enbatis.mybatisplugs.base.BaseServiceImpl;

import com.enbatis.mybatisplugs.commons.sql.Wrapper;
import com.enbatis.mybatisplugs.entity.*;
import com.enbatis.mybatisplugs.entity.vo.TPurchaseCartVO;
import com.enbatis.mybatisplugs.mapper.TPurchaseMapper;
import com.enbatis.mybatisplugs.service.TPurchaseCartService;
import com.enbatis.mybatisplugs.service.TPurchaseItemService;
import com.enbatis.mybatisplugs.service.TPurchaseService;
import com.enbatis.mybatisplugs.service.TRecordService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;

/**
* 描述：
* @author wangweidong
* @date 2019-07-04 19:01:33
*/

@Service
public class TPurchaseServiceImpl extends BaseServiceImpl<TPurchaseMapper,TPurchase > implements TPurchaseService {

    @Autowired
    private TPurchaseCartService tPurchaseCartService;

    @Autowired
    private TPurchaseItemService tPurchaseItemService;
    @Autowired
    private TRecordService recordService;



    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean addPurchaseOrder(Long[] ids, Account sysUser) {
        List<TPurchaseCartVO> tPurchaseCartList=tPurchaseCartService.listVOByIds(ids);
        if (CollectionUtils.isNotEmpty(tPurchaseCartList)){

            BigDecimal cost=BigDecimal.ZERO;
            for ( TPurchaseCartVO cart:tPurchaseCartList) {
                    if (null!=cart.getNum()&&null!=cart.getPrice()){
                        cost=cost.add(cart.getNum().multiply(cart.getPrice()));
                    }
            }

            TPurchase tPurchase=new TPurchase();
            tPurchase.setPurchaseTime(new Date());
            tPurchase.setCode(new Random().nextLong()+"");
            tPurchase.setStatus(0);
            tPurchase.setUname(sysUser.getUsername());
            tPurchase.setCtime(tPurchase.getPurchaseTime() );
            tPurchase.setCompanyId(sysUser.getCompanyId());
            tPurchase.setUid(sysUser.getId());
            tPurchase.setCost(cost);
            insert(tPurchase);

              List<TPurchaseItem> saveList=new ArrayList<>();

            for ( TPurchaseCartVO cart:tPurchaseCartList) {
                TPurchaseItem purchaseItem=new TPurchaseItem();
                purchaseItem.setCompanyId(sysUser.getCompanyId());
                purchaseItem.setCtime(new Date());
                purchaseItem.setDeleted(0);
                purchaseItem.setPrice(cart.getPrice());
                purchaseItem.setProductId(cart.getProductId());
                purchaseItem.setNum(cart.getNum().intValue());
                purchaseItem.setUid(sysUser.getId());
                purchaseItem.setUname(sysUser.getUsername());
                purchaseItem.setPurchaseCartId(cart.getId());
                purchaseItem.setPurchaseId(tPurchase.getId());
                saveList.add(purchaseItem);
            }

            if (CollectionUtils.isNotEmpty(saveList)){
                tPurchaseItemService.saveBatch(saveList);
            }
        }

        TPurchaseCart purchaseCart= new TPurchaseCart();
        purchaseCart.setStatus(1);
        tPurchaseCartService.update(purchaseCart,new Wrapper<>(new TPurchaseCart()).in("id",ids));
        return true;
    }




    @Override
    public boolean toVoid(Long[] ids, String remark, Account sysUser) {

        TPurchase tPurchase=new TPurchase();
        tPurchase.setStatus(4);
        tPurchase.setLid(sysUser.getId());
        tPurchase.setLtime(new Date());
        update(tPurchase,new Wrapper<>(new TPurchase()).in("id",ids));


        List<TRecord> saveList=new ArrayList<>();
        for (Long id:ids) {
            TRecord record=new TRecord();
            record.setBusinessId(id);
            record.setReason(remark);

            saveList.add(record);
        }
        recordService.saveBatch(saveList);
        return true;
    }

}
